package com.info.zhiduoduo.wxsdk.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.info.zhiduoduo.wxsdk.utils.HttpUtil;
import com.info.zhiduoduo.wxsdk.utils.LogUtil;
import com.info.zhiduoduo.wxsdk.utils.StringUtil;

public class WxApi {

	/**
	 登录小程序

	 @param appid  小程序appid
	 @param secret 小程序secret
	 @param wxCode 前端页面的code
	 @return 微信返回的结果
	 */
	public static String loginToWXMiniProgram(String appid, String secret, String wxCode) {
		String apiUrl = WxUrl.getLoginMiniProgramUrl(appid, secret, wxCode);
		String ret = HttpUtil.sendGetWithRetry(apiUrl);
		LogUtil.info("apiurl = " + apiUrl + "\n ret = " + ret);
		return ret;
	}

	/**
	 网页授权方式获取微信用户信息

	 @param accesstoken 网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同
	 @param openid
	 @return
	 */
	public static String getAuthorizeUserInfo(String accesstoken, String openid) {

		String url = WxUrl.getAuthorizeUserinfoUrl(accesstoken, openid);
		LogUtil.info("从微信获取用户信息 url " + url);
		String res = HttpUtil.postDataWithRetry(url, null);
		System.out.println("从微信获取用户信息 res:" + res);
		return res;
	}

	/**
	 普通方式获取微信用户信息

	 @param accesstoken 后台请求的普通token凭证
	 @param openid      用户openid
	 @return
	 */
	public static String getNormalUserInfo(String accesstoken, String openid) {
		String url = WxUrl.getNormalUserInfoUrl(accesstoken, openid);
		LogUtil.info("从微信获取用户信息 url " + url);
		String res = HttpUtil.postDataWithRetry(url, null);
		System.out.println("从微信获取用户信息 res:" + res);
		return res;
	}

	/**
	 网站应用/公众号 获得网页授权信息，转化成map 第二步：通过code换取网页授权access_token
	 网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同
	 微信平台返回数据:
	 {
	 "access_token":"ACCESS_TOKEN",
	 "expires_in":7200,
	 "refresh_token":"REFRESH_TOKEN",
	 "openid":"OPENID",
	 "scope":"SCOPE",
	 "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
	 }

	 @param appid  公众号appid
	 @param secret 公众号秘钥
	 @param code   前端H5页面发送来的code
	 @return
	 */
	public static String getAuthorizeAccessToken(String appid, String secret, String code) {
		String url = WxUrl.getAuthorizeAccessTokenUrl(appid, secret, code);
		LogUtil.info("getAuthorizeAccessToken url : " + url);
		String res = HttpUtil.postDataWithRetry(url, null);
		LogUtil.info("getAuthorizeAccessToken resp : " + res);
		return res;
	}

	/**
	 公众号获取或刷新普通accesstoken

	 @param appid  公众号appid
	 @param secret 公众号秘钥
	 @return
	 */
	public static String getNormalAccessToken(String appid, String secret) {
		String url = WxUrl.getRefreshNormalAccessTokenUrl(appid, secret);
		LogUtil.info("微信 请求accesstoken : " + url);
		String resp = HttpUtil.sendGetWithRetry(url);
		LogUtil.info("微信 请求accesstoken 返回 : " + resp);
		return resp;
	}

	/**
	 获取js凭证

	 @param accessToken
	 @return
	 */
	public static String applyJsTicket(String accessToken) {
		String ticket_url = WxUrl.getJsTicketUrl(accessToken);
		String res = HttpUtil.postDataWithRetry(ticket_url, null);
		if (StringUtil.isEmpty(res)) {
			return "";
		}
		JSONObject ret = JSON.parseObject(res);
		return ret.getString("ticket");
	}

	/**
	 公众号: 发送模板消息

	 @param accesssToken 普通凭证
	 @param data         模板数据.详见WxTempleteMessage类
	 @return 结果json格式:
	 {
	 "errcode":0,
	 "errmsg":"ok",
	 "msgid":200228332
	 }
	 */
	public static String sendTempleteMessage(String accesssToken, String data) {
		String url = WxUrl.getSendTempleteMessageUrl(accesssToken);
		String resp = HttpUtil.postDataWithRetry(url, data);
		LogUtil.info("发送模板消息: url = " + url + " , data = " + data + " ------ 结果 = " + resp);
		return resp;
	}

	/**
	 向指定的openid发送现金红包.

	 @param data xml格式的数据:
	 "<xml>
	 <p>
	 <sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign>
	 <p>
	 <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>
	 <p>
	 <mch_id><![CDATA[888]]></mch_id>
	 <p>
	 <wxappid><![CDATA[wxcbda96de0b165486]]></wxappid>
	 <p>
	 <send_name><![CDATA[send_name]]></send_name>
	 <p>
	 <re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>
	 <p>
	 <total_amount><![CDATA[200]]></total_amount>
	 <p>
	 <total_num><![CDATA[1]]></total_num>
	 <p>
	 <wishing><![CDATA[恭喜发财]]></wishing>
	 <p>
	 <client_ip><![CDATA[127.0.0.1]]></client_ip>
	 <p>
	 <act_name><![CDATA[新年红包]]></act_name>
	 <p>
	 <remark><![CDATA[新年红包]]></remark>
	 <p>
	 <scene_id><![CDATA[PRODUCT_2]]></scene_id>
	 <p>
	 <consume_mch_id><![CDATA[10000097]]></consume_mch_id>
	 <p>
	 <nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str>
	 <p>
	 <risk_info>posttime%3d123123412%26clientversion%3d234134%26mobile%3d122344545%26deviceid%3dIOS</risk_info>
	 <p>
	 </xml>
	 "
	 @return
	 */
	public static String sendRedPacket(String data) {
		String url = WxUrl.getSendRedPacketUrl();
		String resp = HttpUtil.postDataWithRetry(url, data);
		LogUtil.info("发送红包: url = " + url + " , data = " + data + " ------- 结果 = " + resp);
		return resp;
	}
}
